package com.dht.xiao.jianye.dialog;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatSpinner;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;


import com.dht.xiao.jianye.R;

/**
 * Created by Administrator on 2017-08-17.
 */

public class SerialPortDialog {
    //@BindView(R.id.bt_cancle)
    Button btCancle;
    //@BindView(R.id.bt_sure)
    Button btSure;
    TextView txHint;
    private Context context;
    private OnCallBack onCallBack;
    private String hint;


    AppCompatSpinner mSpGradeSerial,mSpGradeBaud;
    public SerialPortDialog(Context context, String hint) {
        this.context = context;
        this.hint = hint;
        DialogInit();
    }

    private void DialogInit() {

        final AlertDialog.Builder Builder =
                new AlertDialog.Builder(context, R.style.DialogHint);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_rfid_ip, null);
       // ButterKnife.bind(this, view);
//        btCancle = (Button)view.findViewById(R.id.bt_cancle);
        btSure = (Button)view.findViewById(R.id.bt_sure);
        txHint = (TextView) view.findViewById(R.id.hint);
        mSpGradeSerial = (AppCompatSpinner) view.findViewById(R.id.sp_grade_Serial);
        mSpGradeBaud = (AppCompatSpinner) view.findViewById(R.id.sp_grade_Baud);

        txHint.setText(hint);
        Builder.setView(view);
        Builder.setCancelable(false);
        final AlertDialog dialog = Builder.show();
        WindowManager.LayoutParams params =
                dialog.getWindow().getAttributes();
        WindowManager manager = ((Activity) context).getWindowManager();
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        params.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.65，根据实际情况调整
        dialog.getWindow().setAttributes(params);

        btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              ///  String aa = mSpGradeSerial.get
                if (onCallBack != null) {
                    onCallBack.sure(SerialPort,BaudRate);
                    dialog.dismiss();
                }
            }
        });

        initView();

    }
    private String[] mStringSerialArray,mStringBaudArray;
    private String SerialPort = "", BaudRate = "";

    private void initView(){
        mStringSerialArray = context.getResources().getStringArray(R.array.Serial);
        mStringBaudArray = context.getResources().getStringArray(R.array.Baud);
        mSpGradeSerial.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                SerialPort= mStringSerialArray[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        mSpGradeBaud.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                BaudRate= mStringBaudArray[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }


    public void setOnCallBackListener(OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }


    public interface OnCallBack {
        public void sure(String SerialPort,String BaudRate);
        public void cancle();
    }
}
